package com.modifiertech.lineage.network;

import com.modifiertech.lineage.BaseConst;
import com.modifiertech.lineage.Lineage;
import com.modifiertech.lineage.api.PlayerData;
import com.modifiertech.lineage.event.PlayerDataHandler;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

/**
 * ClassName: MessageAttributeModify <br/>
 * Description: <br/>
 * date: 2020/8/28 21:41<br/>
 *
 * @author Administrator<br />
 * @since JDK 1.8
 */
public class MessageAttributeModify implements IMessage, IMessageHandler<MessageAttributeModify,IMessage> {
    private NBTTagCompound nbtTagCompound=new NBTTagCompound();

    @Override
    public void fromBytes(ByteBuf byteBuf) {
        nbtTagCompound= ByteBufUtils.readTag(byteBuf);
    }

    @Override
    public void toBytes(ByteBuf byteBuf) {
        ByteBufUtils.writeTag(byteBuf,nbtTagCompound);
    }

    @Override
    public IMessage onMessage(MessageAttributeModify messageAttributeModify, MessageContext messageContext) {
        FMLCommonHandler.instance().getMinecraftServerInstance().addScheduledTask(()->{
            handleMessage(messageAttributeModify,messageContext);
        });
        return null;
    }

    public void handleMessage(MessageAttributeModify messageAttributeModify, MessageContext messageContext){
        //标准消息处理服务端对象获取方式
        PlayerData playerData= PlayerDataHandler.get(messageContext.getServerHandler().player);
        messageAttributeModify.getNbtTagCompound().getKeySet().stream()
                //属性名
                .forEach(h->{
                    //操作类型
                    messageAttributeModify.getNbtTagCompound().getCompoundTag(h).getKeySet().forEach(v->{
                        playerData.optAttr(h,v,messageAttributeModify.getNbtTagCompound().getCompoundTag(h).getInteger(v));
                    });
                });
        //处理完成后更新
        playerData.saveAndSync();
    }

    public NBTTagCompound getNbtTagCompound() {
        return nbtTagCompound;
    }

    public void setNbtTagCompound(NBTTagCompound nbtTagCompound) {
        this.nbtTagCompound = nbtTagCompound;
    }

    public synchronized MessageAttributeModify  write(BaseConst.Attr attr,BaseConst.AttrOperate operate,int num){
        //INIT
        if(!this.getNbtTagCompound().hasKey(attr.name())){
            this.getNbtTagCompound().setTag(attr.name(),new NBTTagCompound());
        }
        //INIT
        if(!this.getNbtTagCompound().getCompoundTag(attr.name()).hasKey(operate.name())){
            this.getNbtTagCompound().getCompoundTag(attr.name()).setInteger(operate.name(),num);
        }else{
            this.getNbtTagCompound().getCompoundTag(attr.name()).setInteger(operate.name(),
                    this.getNbtTagCompound().getCompoundTag(attr.name()).getInteger(operate.name())+num);
        }
        return this;
    }

}
